#include <iostream>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

typedef struct time{
    int hour;
    int minute;
    int second;
}Time;

///void qsort(void *base, size_t num, size_t size,
///           int (*comparator) (const void*, const void *));

typedef int (*FuncPtr) (const void*, const void *);

int compare_int(const void *pa, const void *pb)
{
    return *((int*)pa) - *((int*)pb);
}

int compare_double(const void *pa, const void *pb)
{
    double a = *((double*)pa);
    double b = *((double*)pb);
    if(a > b) return 1;
    else if(a < b) return -1;
    else return 0;
//    return *((double*)pa) > *((double*)pb);
}

int compare_string(const void *pa, const void *pb)
{
    return strcmp((char*)pa, (char*)pb);
}

int compare_time(const void *pa, const void *pb)
{
    Time a = *((Time*)pa);
    Time b = *((Time*)pb);
    if(a.hour*3600+a.minute*60+a.second > b.hour*3600+b.minute*60+b.second) return 1;
    else if(a.hour*3600+a.minute*60+a.second < b.hour*3600+b.minute*60+b.second) return -1;
    else return 0;
}

int main()
{
    int arr_i[10] = {3,7,5,4,1,2,8,9,0,6};
    FuncPtr pFunc = compare_int;
    qsort(arr_i, 10, sizeof(int), pFunc);
    for(int i = 0; i < 10; i++)
        printf("%d ", arr_i[i]);
    printf("\n");

    double arr_d[10] = {2.3,6.7,5.2,4.8,1.0,2.9,8.5,9.4,0.123,6.8};
    pFunc = compare_double;
    qsort(arr_d, 10, sizeof(double), pFunc);
    for(int i = 0; i < 10; i++)
        printf("%.2f ", arr_d[i]);
    printf("\n");

    char arr_s[5][10] = {"hello", "world", "red", "black", "white"};
    pFunc = compare_string;
    qsort(arr_s, 5, sizeof(char)*10, (FuncPtr)strcmp);
    for(int i = 0; i < 5; i++)
        printf("%s ", arr_s[i]);
    printf("\n");

    Time arr_t[5] = {{12,25,16}, {5,16,54}, {21,12,58}, {2,12,54}, {8,14,32}};
    pFunc = compare_time;
    qsort(arr_t, 5, sizeof(Time), pFunc);
    for(int i = 0; i < 5; i++)
        printf("[%d:%d:%d]  ", arr_t[i].hour, arr_t[i].minute, arr_t[i].second);
    printf("\n");

    return 0;
}




















